package com.tanhua.server.controller;

import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

/**
 *  发布动态
 * */
    @PostMapping
    public ResponseEntity movements(MultipartFile imageContent[],String textContent, String location , String longitude, String latitude) throws IOException {
        //保存
        movementsService.save(imageContent,textContent,location,longitude,latitude);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询个人动态
     * */
    @GetMapping("/all")
    public ResponseEntity all(@RequestParam(defaultValue = "1")Integer page,
                              @RequestParam(defaultValue = "10")Integer pagesize,
                              Long userId){
        //查询个人
        PageResult vo =   movementsService.all(userId,page,pagesize);
        return ResponseEntity.ok(vo);


    }
    /**
     * 查询好友动态
     * */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1")Integer page,
                                    @RequestParam(defaultValue = "10")Integer pagesize){

        //查询好友
        PageResult vo =   movementsService.findByFriendsId(page,pagesize);
        return ResponseEntity.ok(vo);
    }
    /**
     * 推荐动态
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1")Integer page,
                                    @RequestParam(defaultValue = "10")Integer pagesize){
        PageResult vo = movementsService.findByRecommend(page,pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 根据id查询动态
     * */
    @GetMapping("{id}")
    public ResponseEntity findById(@PathVariable("id") String id){
     MovementsVo vo =  movementsService.findById(id);
    return ResponseEntity.ok(vo);
    }
    /**
     * 首页端 访客记录
     * */
    @GetMapping("visitors")
    public ResponseEntity visitors(){
        return ResponseEntity.ok(null);
    }

    /**
     * 点赞
     * */
    @GetMapping("{id}/like")
    public ResponseEntity like(@PathVariable("id") String id){

       Integer count =  movementsService.like(id);
        return ResponseEntity.ok(count);
    }
    /**
     * 取消点赞
     * */
    @GetMapping("{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String id,
                                  @RequestParam(defaultValue = "10")Integer pagesize){

       Integer count =  movementsService.dislike(id);
        return ResponseEntity.ok(count);
    }
    /**
     * 喜欢
     * */
    //喜欢
    @GetMapping("/{movementId}/love")
    public ResponseEntity love(@PathVariable("movementId") String movementId) {
        Integer count = movementsService.love(movementId);
        return ResponseEntity.ok(count);
    }
    /**
     * 取消喜欢
     */
    @GetMapping("/{movementId}/unlove")
    public ResponseEntity unlove(@PathVariable("movementId") String movementId) {
        Integer count = movementsService.unlove(movementId);
        return ResponseEntity.ok(count);
    }
}
